﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;

namespace GRWL.BZSC.UI
{
    public partial class EditQcOptionFrm : BaseFrm
    {
        public bool CallDone { get; set; }

        public TB_QCProblemType QCProblemType { get; set; }
        public TB_QCResult QCResult { get; set; }

        private enum EditDataType { enPromble, enResult };
        private EditDataType TableType;

        private enum EditMode { emAppend, emUpdate };
        private EditMode frmEditMode;

        public int RecId { get; set; }
        public string RecDesc { get; set; }
        public bool RecFlag { get; set; }

        public EditQcOptionFrm()
        {
            InitializeComponent();

            QCProblemType = new TB_QCProblemType();
            QCResult = new TB_QCResult();

            this.BaseDataProxy = new BaseDataClient();
            this.BaseDataProxy.Open();
        }

        private void EditQcOptionFrm_Load(object sender, EventArgs e)
        {
            //编辑记录的内容绑定到对应控件
            if (RecId > 0)
                CodeUTextBox.Text = RecId.ToString();
            DescUTextBox.Text = RecDesc;
            FlagCheckBox.Checked = RecFlag;

            switch (TableType)
            {
                case EditDataType.enPromble:
                    this.Text = "质量问题";
                    return;
                case EditDataType.enResult:
                    this.Text = "质量结果";
                    return;
            }
        }

        public void SetEditAsPromble()
        {
            TableType = EditDataType.enPromble;
        }

        public void SetEditAsResult()
        {
            TableType = EditDataType.enResult;
        }

        public void SetEditModeAppend()
        {
            frmEditMode = EditMode.emAppend;
        }

        public void SetEditModeUpdate()
        {
            frmEditMode = EditMode.emUpdate;
        }

        public bool IsEditModeAppend()
        {
            return (frmEditMode == EditMode.emAppend);
        }

        public bool IsEditModeUpdate()
        {
            return (frmEditMode == EditMode.emUpdate);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            RecDesc = DescUTextBox.Text;
            RecFlag = FlagCheckBox.Checked;
            switch (TableType)
            {
                case EditDataType.enPromble:
                    //编辑记录的内容绑定到对应控件
                    QCProblemType.id = Convert.ToInt16(CodeUTextBox.Text);
                    QCProblemType.name = DescUTextBox.Text;
                    QCProblemType.isScrap = FlagCheckBox.Checked;
                    try
                    {
                        if (IsEditModeAppend())
                        {
                            int g = BaseDataProxy.AddQCProblemType(QCProblemType);
                            if (g > 0)
                            {
                                MessageBox.Show("已成功保存记录.");
                                CallDone = true;
                                Close();
                            }
                        }
                        else
                        {
                            if (BaseDataProxy.UpdateQCProblemType(QCProblemType))
                            {
                                MessageBox.Show("已成功保存记录.");
                                CallDone = true;
                                Close();
                            }
                        }
                    }
                    catch (InvalidCastException eMsg)
                    {
                        MessageBox.Show(eMsg.Message.ToString());
                    }
                    return;
                case EditDataType.enResult:
                    QCResult.id = Convert.ToInt16(CodeUTextBox.Text);
                    QCResult.result = DescUTextBox.Text;
                    QCResult.passFlag = FlagCheckBox.Checked;
                    try
                    {
                        if (IsEditModeAppend())
                        {
                            int g = BaseDataProxy.AddQCResult(QCResult);
                            if (g > 0)
                            {
                                MessageBox.Show("已成功保存记录.");
                                CallDone = true;
                                Close();
                            }
                        }
                        else
                        {
                            if (BaseDataProxy.UpdateQCResult(QCResult))
                            {
                                MessageBox.Show("已成功保存记录.");
                                CallDone = true;
                                Close();
                            }
                        }
                    }
                    catch (InvalidCastException eMsg)
                    {
                        MessageBox.Show(eMsg.Message.ToString());
                    }
                    return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
